<template>
  <div class="chat-container">
    <div class="chat-header">
      <el-button @click="$router.push('/')" class="back-btn">
        <i class="el-icon-arrow-left"></i>
        返回
      </el-button>
      <div class="header-content">
        <h2>AI 超级智能体</h2>
      </div>
    </div>
    
    <div class="chat-messages" ref="messageContainer">
      <div v-for="(msg, index) in messages" :key="index" 
           :class="['message', msg.type === 'user' ? 'user-message' : 'ai-message']">
        <div class="message-content">
          <div class="avatar">
            <img v-if="msg.type === 'ai'" src="https://api.dicebear.com/7.x/bottts/svg?seed=agent" alt="AI" />
            <img v-else src="https://api.dicebear.com/7.x/personas/svg?seed=user" alt="User" />
          </div>
          <div class="text">{{ msg.content }}</div>
        </div>
        <div class="message-time">{{ getCurrentTime() }}</div>
      </div>
    </div>

    <div class="chat-input">
      <el-input
        v-model="inputMessage"
        placeholder="请输入您的问题..."
        :disabled="loading"
        @keyup.enter="sendMessage"
        class="message-input"
      >
        <template #append>
          <el-button type="primary" :loading="loading" @click="sendMessage">
            发送
          </el-button>
        </template>
      </el-input>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import { sendMessageToAI } from '../api'

const messages = ref([])
const inputMessage = ref('')
const loading = ref(false)
const messageContainer = ref(null)

function getCurrentTime() {
  const now = new Date()
  return now.toLocaleTimeString('zh-CN', { hour12: false })
}

async function sendMessage() {
  if (!inputMessage.value.trim() || loading.value) return
  
  const userMessage = inputMessage.value
  messages.value.push({ type: 'user', content: userMessage })
  inputMessage.value = ''
  loading.value = true

  try {
    const reader = await sendMessageToAI(userMessage)
    let aiResponse = ''

    while (true) {
      const { value, done } = await reader.read()
      if (done) break
      
      const chunk = new TextDecoder().decode(value)
      const lines = chunk.split('\n')
      
      for (const line of lines) {
        if (line.startsWith('data:')) {
          const data = line.slice(5).trim()
          if (data === '[DONE]') {
            loading.value = false
            return
          }
          
          aiResponse += data + '\n'
          const lastMessage = messages.value[messages.value.length - 1]
          
          if (lastMessage && lastMessage.type === 'ai') {
            lastMessage.content = aiResponse
          } else {
            messages.value.push({ type: 'ai', content: aiResponse })
          }
          
          scrollToBottom()
        }
      }
    }
  } catch (error) {
    console.error('Error:', error)
    loading.value = false
  }
}

function scrollToBottom() {
  nextTick(() => {
    if (messageContainer.value) {
      messageContainer.value.scrollTop = messageContainer.value.scrollHeight
    }
  })
}

onMounted(() => {
  messages.value.push({
    type: 'ai',
    content: '你好！我是AI超级智能体，可以回答您的任何问题。'
  })
})
</script>

<style scoped>
.chat-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: white;
  position: relative;
  overflow: hidden;
  max-width: 50%;
  margin: 0 auto;
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.1);
}

.chat-container::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  background: repeating-linear-gradient(
    45deg,
    rgba(0, 255, 255, 0.03) 0px,
    rgba(0, 255, 255, 0.03) 1px,
    transparent 1px,
    transparent 10px
  );
  animation: bg-scroll 20s linear infinite;
}

@keyframes bg-scroll {
  0% {
    transform: translate(-50%, -50%) rotate(0deg);
  }
  100% {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

.chat-header {
  padding: 15px 20px;
  background-color: var(--bg-secondary);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 20px;
  z-index: 10;
}

.header-content {
  flex: 1;
  text-align: center;
}

.header-content h2 {
  margin: 0;
  color: var(--primary-color);
  font-size: 1.5rem;
}

.back-btn {
  padding: 10px 15px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  scroll-behavior: smooth;
}

.message {
  margin-bottom: 20px;
  max-width: 80%;
}

.message-content {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.text {
  padding: 12px 16px;
  border-radius: 10px;
  background-color: var(--bg-secondary);
  box-shadow: var(--shadow);
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  text-align: left;
}

.message-time {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 5px;
  margin-left: 52px;
}

.user-message {
  margin-left: auto;
  
  .message-content {
    flex-direction: row-reverse;
  }
  
  .text {
    background-color: var(--primary-color);
    color: #fff;
  }
  
  .message-time {
    text-align: right;
    margin-left: 0;
    margin-right: 52px;
  }
}

.chat-input {
  padding: 20px;
  background-color: var(--bg-secondary);
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}

.message-input {
  :deep(.el-input__wrapper) {
    padding-right: 0;
  }
  
  :deep(.el-input-group__append) {
    padding: 0;
  }
  
  :deep(.el-button) {
    border: none;
    padding: 8px 20px;
    height: 100%;
  }
}

@media (max-width: 768px) {
  .chat-container {
    max-width: 90%;
  }
  
  .message {
    max-width: 90%;
  }
  
  .chat-header {
    padding: 10px 15px;
  }
  
  .header-content h2 {
    font-size: 1.2rem;
  }
  
  .chat-input {
    padding: 15px;
  }
}

@media (max-width: 480px) {
  .chat-container {
    max-width: 95%;
  }
  
  .message {
    max-width: 95%;
  }
  
  .avatar {
    width: 32px;
    height: 32px;
  }
  
  .text {
    font-size: 13px;
    padding: 10px 12px;
  }
  
  .message-time {
    margin-left: 44px;
  }
  
  .user-message .message-time {
    margin-right: 44px;
  }
}
</style> 